package io.github.talelin.latticy.controller.v1;


import io.github.talelin.core.annotation.*;
import io.github.talelin.latticy.dto.admin.ResetPasswordDTO;
import io.github.talelin.latticy.dto.omsuser.CreateOmsUserDTO;
import io.github.talelin.latticy.dto.omsuser.UpdateOmsUserDTO;
import io.github.talelin.latticy.dto.query.BasePageDTO;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.service.WmsOmsUserRelService;
import io.github.talelin.latticy.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * @author generator@wulongbo
 * @since 2022-05-28
 */
@RestController
@RequestMapping("/v1/wms-oms-user-rel")
@PermissionModule(value = "oms用户管理")
@Validated
public class WmsOmsUserRelController {

    @Autowired
    private WmsOmsUserRelService wmsOmsUserRelService;

    @GroupRequired
    @PostMapping("")
    @PermissionMeta(value = "添加用户")
    @Logger(template = "{user.username}添加oms用户:{request.username}")
    public CreatedVO create(@RequestBody @Valid CreateOmsUserDTO validator) {
        wmsOmsUserRelService.createOmsUser(validator);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "编辑用户")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id,
                            @RequestBody @Validated UpdateOmsUserDTO validator) {
        return wmsOmsUserRelService.updateOmsUser(id, validator);
    }

    @GroupRequired
    @PutMapping("/{id}/password")
    @PermissionMeta(value = "修改密码")
    @Logger(template = "{user.username}修改oms用户:{request.id}的密码")
    public UpdatedVO changeUserPassword(@PathVariable @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated ResetPasswordDTO validator) {
        return wmsOmsUserRelService.changeUserPassword(id, validator);
    }

    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除用户")
    @Logger(template = "{user.username}删除oms用户:{request.id}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        wmsOmsUserRelService.deleteOmsUser(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    @LoginRequired
    public UserDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return wmsOmsUserRelService.getUserById(id);
    }

    @GetMapping("/list")
    @GroupRequired
    @PermissionMeta(value = "用户列表")
    public FindResultVO list() {
        return FindResultVO.success(wmsOmsUserRelService.getUserOmsList());
    }

    @GetMapping("/page")
    @GroupRequired
    @PermissionMeta(value = "用户分页")
    public PageResponseVO<UserDO> page(@Validated BasePageDTO dto) {
        return wmsOmsUserRelService.getUserOmsPage(dto);
    }

}
